package pers.li.utils;

import java.io.*;
import java.util.Properties;

public class PropertiesUtils {
    private static Properties properties = null;

    /**
     * @param key   properties中的键
     * @return      对应的value值
     */
    public static String getValue(String key) {
        if (properties == null) {
            properties = loadConfProperties();
        }
        String value = properties.getProperty(key);
        return value;
    }

    /**
     * @return 相应路径下properties文件
     */
    public static Properties loadConfProperties() {
        Properties properties = new Properties();
        InputStream in = null;

        String confPath = System.getProperty("user.dir");
        confPath = confPath + File.separator + "database.properties";
        File file = new File(confPath);
        if (file.exists()) {
            try {
                in = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        else {
            in = PropertiesUtils.class.getClassLoader().getResourceAsStream("JDBC.properties");
        }
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
}
